//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "HestonHullWhitePathPricer.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Payoff.h>
#include <gen/QL/Processes/HybridHestonHullWhiteProcess.h>
#include <gen/QL/Methods/Montecarlo/MultiPath.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Methods::Montecarlo;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::CHestonHullWhitePathPricer (Double exerciseTime, Cephei::QL::IPayoff^ payoff, Cephei::QL::Processes::IHybridHestonHullWhiteProcess^ process) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CPayoff^ _Cpayoff;
    CHybridHestonHullWhiteProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phHestonHullWhitePathPricer = NULL;
#endif
        QuantLib::Time _exerciseTime = (QuantLib::Time)ValueHelper::Convert (exerciseTime); //d
        _Cpayoff = safe_cast<CPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::Payoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::Payoff>&> (_Cpayoff->GetShared ()); 
        _Cprocess = safe_cast<CHybridHestonHullWhiteProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess>& _process = static_cast<boost::shared_ptr<QuantLib::HybridHestonHullWhiteProcess>&> (_Cprocess->GetShared ()); 
        _ppHestonHullWhitePathPricer = new boost::shared_ptr<QuantLib::HestonHullWhitePathPricer> (new QuantLib::HestonHullWhitePathPricer ( _exerciseTime,  _payoff,  _process ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::CHestonHullWhitePathPricer (boost::shared_ptr<QuantLib::HestonHullWhitePathPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHestonHullWhitePathPricer = NULL;
#endif
	_ppHestonHullWhitePathPricer = &childNative;
    
}
Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::CHestonHullWhitePathPricer (QuantLib::HestonHullWhitePathPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phHestonHullWhitePathPricer = NULL;
#endif
	_ppHestonHullWhitePathPricer = new boost::shared_ptr<QuantLib::HestonHullWhitePathPricer> (&childNative);
    
    _HestonHullWhitePathPricerOwner = owner;
    
}

Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::CHestonHullWhitePathPricer (CHestonHullWhitePathPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHestonHullWhitePathPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHestonHullWhitePathPricer = new boost::shared_ptr<QuantLib::HestonHullWhitePathPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::CHestonHullWhitePathPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHestonHullWhitePathPricer = NULL;
#endif
	if (!t->IsSubclassOf(CHestonHullWhitePathPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::CHestonHullWhitePathPricer (QuantLib::Handle<QuantLib::HestonHullWhitePathPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phHestonHullWhitePathPricer = &childNative;
	_ppHestonHullWhitePathPricer = &static_cast<boost::shared_ptr<QuantLib::HestonHullWhitePathPricer>>(childNative.currentLink());
    
    _HestonHullWhitePathPricerOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::CHestonHullWhitePathPricer (QuantLib::Handle<QuantLib::HestonHullWhitePathPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phHestonHullWhitePathPricer = &childNative;
	_ppHestonHullWhitePathPricer = &static_cast<boost::shared_ptr<QuantLib::HestonHullWhitePathPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::CHestonHullWhitePathPricer (QuantLib::HestonHullWhitePathPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHestonHullWhitePathPricer = NULL;
#endif
	_ppHestonHullWhitePathPricer = new boost::shared_ptr<QuantLib::HestonHullWhitePathPricer> (new QuantLib::HestonHullWhitePathPricer (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::~CHestonHullWhitePathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppHestonHullWhitePathPricer != NULL)
    {
	    delete _ppHestonHullWhitePathPricer;
        _ppHestonHullWhitePathPricer = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::!CHestonHullWhitePathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppHestonHullWhitePathPricer != NULL)
    {
	    delete _ppHestonHullWhitePathPricer;
    }
}
QuantLib::HestonHullWhitePathPricer& Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::GetReference ()
{
    if (_ppHestonHullWhitePathPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppHestonHullWhitePathPricer;
}
boost::shared_ptr<QuantLib::HestonHullWhitePathPricer>& Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::GetShared ()
{
    if (_ppHestonHullWhitePathPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppHestonHullWhitePathPricer;
}
QuantLib::HestonHullWhitePathPricer* Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::GetPointer ()
{
    if (_ppHestonHullWhitePathPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppHestonHullWhitePathPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HestonHullWhitePathPricer>& Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::GetHandle ()
{
	if (_phHestonHullWhitePathPricer == NULL)
	{
		_phHestonHullWhitePathPricer = new Handle<QuantLib::HestonHullWhitePathPricer> (*_ppHestonHullWhitePathPricer);
	}
	return *_phHestonHullWhitePathPricer;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer::HasNative () 
{
	return (_ppHestonHullWhitePathPricer != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IHestonHullWhitePathPricer^ Cephei::QL::Pricingengines::Vanilla::CHestonHullWhitePathPricer_Factory::Create (Double exerciseTime, Cephei::QL::IPayoff^ payoff, Cephei::QL::Processes::IHybridHestonHullWhiteProcess^ process)
{
    return REFNEW CHestonHullWhitePathPricer ( exerciseTime,  payoff,  process);
}
